/*
 * datalink.h
 *
 *  Created on: 2016-3-30
 *      Author: Administrator
 */

#ifndef DATALINK_H_
#define DATALINK_H_

#include"utils.h"

static int NewUdpSocket(JNIEnv* env, jobject obj) {

	//构造socket

	int udpSocket = socket(PF_INET, SOCK_DGRAM, 0);
	return udpSocket;
}

#define ERRORIP "cannot find host ip"

char *ip_search(void)
{
    int sfd, intr;
    struct ifreq buf[16];
    struct ifconf ifc;
    sfd = socket (AF_INET, SOCK_DGRAM, 0);
    if (sfd < 0)
        return ERRORIP;
    ifc.ifc_len = sizeof(buf);
    ifc.ifc_buf = (caddr_t)buf;
    if (ioctl(sfd, SIOCGIFCONF, (char *)&ifc))
        return ERRORIP;
    intr = ifc.ifc_len / sizeof(struct ifreq);
    while (intr-- > 0 && ioctl(sfd, SIOCGIFADDR, (char *)&buf[intr]));
    close(sfd);
    return inet_ntoa(((struct sockaddr_in*)(&buf[intr].ifr_addr))-> sin_addr);
}

static void createBroadcastIp(char* ip){

	int k=0;
	for(int i=0;i<12;i++){
		LOGI("%c",ip[i]);
		if(ip[i]=='.'){
			k++;
		}
		if(k==3){
			ip[i+1]=50;
			ip[i+2]=53;
			ip[i+3]=53;
			return;
			}
	}


}

static void scanfBlinkRouteAddress1(int* socket, struct sockaddr_in* sendaddress,
		struct sockaddr_in* revicesaddress,char* mip, int port) {
	LOGI("ip===%s",mip);
	sendaddress->sin_family = AF_INET;
	sendaddress->sin_addr.s_addr = inet_addr(mip);
	sendaddress->sin_port = htons(port);
	revicesaddress->sin_family = AF_INET;
	revicesaddress->sin_port = htons(port);
	revicesaddress->sin_addr.s_addr = htonl(INADDR_ANY);
	setsockopt(*socket, SOL_SOCKET, SO_BROADCAST, sendaddress,
			sizeof(*sendaddress));
}

static void scanfBlinkRouteAddress(int* socket, struct sockaddr_in* sendaddress,
		struct sockaddr_in* revicesaddress,char* mip, int port) {
	createBroadcastIp(mip);
	LOGI("ip===%s",mip);
	sendaddress->sin_family = AF_INET;
	sendaddress->sin_addr.s_addr = inet_addr(mip);
	sendaddress->sin_port = htons(port);
	revicesaddress->sin_family = AF_INET;
	revicesaddress->sin_port = htons(port);
	revicesaddress->sin_addr.s_addr = htonl(INADDR_ANY);
	setsockopt(*socket, SOL_SOCKET, SO_BROADCAST, sendaddress,
			sizeof(*sendaddress));
}

static int NewScanfSocket(JNIEnv *env, jobject obj) {
	int s = NewUdpSocket(env, obj);
	return s;
}

static jstring scanfBlink(JNIEnv *env, jobject obj, jstring mip,char* message) {
	int so_broadcast = 1;
	struct sockaddr_in my_addr, user_addr;
	char msgbuf[MAX_LINE], buffer[MAX_LINE];
	int datalen, time = 0,p=0;
	//最多容纳10台
	char desip[120];
	int socket = NewScanfSocket(env, obj);
	if (NULL != env->ExceptionOccurred()) {
		ThrowException(env, "socket is have");
		close(socket);
	}
	const char* ip = env->GetStringUTFChars(mip, NULL);
	char boradcast[12]="\0";
	strcpy(boradcast,ip);
	scanfBlinkRouteAddress(&socket, &my_addr, &user_addr,boradcast,port);
	SendDatagramToSocket(env, obj, socket, &my_addr, message, strlen(message));
	env->ReleaseStringUTFChars(mip,ip);
	memset(&my_addr, 0, sizeof(my_addr));
	while (time < 5) {
		int i = ReceiveDatagramFromSocket(env, obj, socket, &user_addr, buffer,
				MAX_BUFFER_SIZE);
		if (i == -1) {
			break;
		}
		time++;
		LOGI("%d",i);
		strcpy(desip+p, (const char*)inet_ntoa(user_addr.sin_addr));
		desip[strlen(desip)]='@';
		p+=strlen(desip);
	}
	close(socket);
	return env->NewStringUTF(desip);
}

static jstring sendudp(JNIEnv *env, jobject obj, jstring mip,jstring message){
	int so_broadcast = 1;
	struct sockaddr_in my_addr, user_addr;
	char msgbuf[MAX_LINE], buffer[MAX_LINE];
	int datalen, time = 0,p=0;
	//最多容纳10台
	char desip[120];
	int socket = NewScanfSocket(env, obj);
	if (NULL != env->ExceptionOccurred()) {
		ThrowException(env, "socket is have");
		close(socket);
	}
	const char* ip = env->GetStringUTFChars(mip, NULL);
	char boradcast[12]="\0";
	strcpy(boradcast,ip);
	const char* messeText = env->GetStringUTFChars(message, NULL);
	scanfBlinkRouteAddress1(&socket, &my_addr, &user_addr,boradcast,port);
	if (NULL == messeText) {
		exit(0);
	}
	jsize messageSize = env->GetStringUTFLength(message);
	SendDatagramToSocket(env, obj, socket, &my_addr, messeText, messageSize);
	env->ReleaseStringUTFChars(message, messeText);
	env->ReleaseStringUTFChars(mip,ip);
	memset(&my_addr, 0, sizeof(my_addr));
	while (time < 5) {
		int i = ReceiveDatagramFromSocket(env, obj, socket, &user_addr, buffer,
				MAX_LINE);
		if (i == -1) {
			break;
		}
		time++;
		LOGI("%d buff==%s",i,buffer);
		strcpy(desip+p, (const char*)inet_ntoa(user_addr.sin_addr));
		desip[strlen(desip)]='@';
		p+=strlen(desip);
	}
	if(ip!=NULL){
		delete[] ip;
	}
	close(socket);
	return env->NewStringUTF(desip);
}

static jstring sendudpchar(JNIEnv *env, jobject obj, jstring mip,char* message){
	int so_broadcast = 1;
	struct sockaddr_in my_addr, user_addr;
	char msgbuf[MAX_LINE], buffer[MAX_LINE];
	int datalen, time = 0,p=0;
	//最多容纳10台
	char desip[120];
	int socket = NewScanfSocket(env, obj);
	if (NULL != env->ExceptionOccurred()) {
		ThrowException(env, "socket is have");
		close(socket);
	}
	const char* ip = env->GetStringUTFChars(mip, NULL);
	char boradcast[12]="\0";
	strcpy(boradcast,ip);
	scanfBlinkRouteAddress1(&socket, &my_addr, &user_addr,boradcast,port);
	SendDatagramToSocket(env, obj, socket, &my_addr,message,strlen(message));
	env->ReleaseStringUTFChars(mip,ip);
	memset(&my_addr, 0, sizeof(my_addr));
	while (time < 5) {
		int i = ReceiveDatagramFromSocket(env, obj, socket, &user_addr, buffer,
				MAX_LINE);
		if (i == -1) {
			break;
		}
		time++;
		LOGI("%d buff==%s",i,buffer);
		strcpy(desip+p, (const char*)inet_ntoa(user_addr.sin_addr));
		desip[strlen(desip)]='@';
		p+=strlen(desip);
	}
	close(socket);
	if(ip!=NULL){
		delete[] ip;
	}
	return env->NewStringUTF(desip);
}

static char* dataCreateSendString(const char* data){

	int len=strlen(data);
	LOGI("len==%d",len);
	//生成头
	char* head=intToChar(len);
	LOGI("生成头==%s",head);
	char* res=strcat(head,data);
	LOGI("生成res==%s",res);
	return res;
}

static char* jsonCreateGetTerminallist(){
	 cJSON * pJsonRoot = NULL;
	 pJsonRoot=cJSON_CreateObject();
	 cJSON_AddStringToObject(pJsonRoot, "type", "getterminallist");
	 char* c=cJSON_Print(pJsonRoot);
	 cJSON_Delete(pJsonRoot);
	 return c;
}

static char* jsonCreategetwancfg(){
	 cJSON * pJsonRoot = NULL;
	 pJsonRoot=cJSON_CreateObject();
	 cJSON_AddStringToObject(pJsonRoot, "type", "getwancfg");
	 char* c=cJSON_Print(pJsonRoot);
	 cJSON_Delete(pJsonRoot);
	 return c;
}

static char* jsonCreateCFG(char* type){
	 cJSON * pJsonRoot = NULL;
	 pJsonRoot=cJSON_CreateObject();
	 cJSON_AddStringToObject(pJsonRoot, "type", type);
	 char* c=cJSON_Print(pJsonRoot);
	 cJSON_Delete(pJsonRoot);
	 if(c!=NULL){
		 delete[] type;
	 }
	 return c;
}

static char* jsonCreatesethostname(const char* name){
	 cJSON * pJsonRoot = NULL;
	 pJsonRoot=cJSON_CreateObject();
	 cJSON_AddStringToObject(pJsonRoot, "type","sethostname");
	 cJSON_AddStringToObject(pJsonRoot, "hostname",name);
	 char* c=cJSON_Print(pJsonRoot);
	 cJSON_Delete(pJsonRoot);
	 if(c!=NULL){
		 delete[] name;
	 }
	 return c;
}

static char* jsonCreateGetlancfg(){
	 cJSON * pJsonRoot = NULL;
	 pJsonRoot=cJSON_CreateObject();
	 cJSON_AddStringToObject(pJsonRoot, "type", "getlancfg");
	 char* c=cJSON_Print(pJsonRoot);
	 cJSON_Delete(pJsonRoot);
	 return c;
}

static char* jsonCreatesetvisitorinfo(const char* gssid,const char* gpsw,int gssidenable){
	 cJSON * pJsonRoot = NULL;
	 pJsonRoot=cJSON_CreateObject();
	 cJSON_AddStringToObject(pJsonRoot, "type", "setvisitorinfo");
	 cJSON_AddStringToObject(pJsonRoot, "gssid", gssid);
	 cJSON_AddStringToObject(pJsonRoot, "type",gpsw);
	 cJSON_AddNumberToObject(pJsonRoot, "type",gssidenable);
	 char* c=cJSON_Print(pJsonRoot);
	 cJSON_Delete(pJsonRoot);
	 return c;
}

static char* jsonCreategetwlancfg(){
	 cJSON * pJsonRoot = NULL;
	 pJsonRoot=cJSON_CreateObject();
	 cJSON_AddStringToObject(pJsonRoot, "type", "getwlancfg");
	 char* c=cJSON_Print(pJsonRoot);
	 cJSON_Delete(pJsonRoot);
	 return c;
}

static char* jsonCreatesetswitchwifi(int i){
	 cJSON * pJsonRoot = NULL;
	 pJsonRoot=cJSON_CreateObject();
	 cJSON_AddStringToObject(pJsonRoot, "type", "setswitchwifi");
	 cJSON_AddNumberToObject(pJsonRoot, "radioon",i);
	 char* c=cJSON_Print(pJsonRoot);
	 cJSON_Delete(pJsonRoot);
	 return c;
}

static char* jsonCreateupgrade(int i,const char* url){
	 cJSON * pJsonRoot = NULL;
	 pJsonRoot=cJSON_CreateObject();
	 cJSON_AddStringToObject(pJsonRoot, "type", "upgrade");
	 cJSON_AddNumberToObject(pJsonRoot, "isforce",i);
	 cJSON_AddStringToObject(pJsonRoot, "url",url);
	 char* c=cJSON_Print(pJsonRoot);
	 cJSON_Delete(pJsonRoot);
	 return c;
}

static char* jsonCreateGetQos(const char* mac){
	 cJSON * pJsonRoot = NULL;
	 pJsonRoot=cJSON_CreateObject();
	 cJSON_AddStringToObject(pJsonRoot, "type", "getqos");
	 cJSON_AddStringToObject(pJsonRoot, "MAC",mac);
	 char* c=cJSON_Print(pJsonRoot);
	 cJSON_Delete(pJsonRoot);
	 return c;
}

static char* jsonCreatesetqos(const char* mac,const char* IP,const char* u_speed,const char* d_speed){
	 cJSON * pJsonRoot = NULL;
	 pJsonRoot=cJSON_CreateObject();
	 cJSON_AddStringToObject(pJsonRoot, "type", "setqos");
	 cJSON_AddStringToObject(pJsonRoot, "MAC",mac);
	 cJSON_AddStringToObject(pJsonRoot, "IP",IP);
	 cJSON_AddStringToObject(pJsonRoot, "Up_Speed",u_speed);
	 cJSON_AddStringToObject(pJsonRoot, "Dl_Speed",d_speed);
	 char* c=cJSON_Print(pJsonRoot);
	 cJSON_Delete(pJsonRoot);
	 return c;
}

static char* jsonCreatesetmacblack(const char* s,int i){
	 cJSON * pJsonRoot = NULL;
	 pJsonRoot=cJSON_CreateObject();
	 cJSON_AddStringToObject(pJsonRoot, "type", "setmacblack");
	 cJSON_AddNumberToObject(pJsonRoot, "enable",i);
	 cJSON_AddStringToObject(pJsonRoot, "mac",s);
	 char* c=cJSON_Print(pJsonRoot);
	 cJSON_Delete(pJsonRoot);
	 return c;
}

static char* jsonCreatesetpamode(int i){
	 cJSON * pJsonRoot = NULL;
	 pJsonRoot=cJSON_CreateObject();
	 cJSON_AddStringToObject(pJsonRoot, "type", "setpamode");
	 cJSON_AddNumberToObject(pJsonRoot, "radioon",i);
	 char* c=cJSON_Print(pJsonRoot);
	 cJSON_Delete(pJsonRoot);
	 return c;
}

static char* jsonCreatesetchannel(int i){
	 cJSON * pJsonRoot = NULL;
	 pJsonRoot=cJSON_CreateObject();
	 cJSON_AddStringToObject(pJsonRoot, "type", "setchannel");
	 cJSON_AddNumberToObject(pJsonRoot, "channel",i);
	 char* c=cJSON_Print(pJsonRoot);
	 cJSON_Delete(pJsonRoot);
	 return c;
}

static char* jsonCreatesetmanpwd(const char* pw){
	 cJSON * pJsonRoot = NULL;
	 pJsonRoot=cJSON_CreateObject();
	 cJSON_AddStringToObject(pJsonRoot, "type", "setmanpwd");
	 cJSON_AddStringToObject(pJsonRoot, "routepwd",pw);
	 char* c=cJSON_Print(pJsonRoot);
	 cJSON_Delete(pJsonRoot);
	 return c;
}

static char* jsonGetHostinfo(){

	 cJSON * pJsonRoot = NULL;
	 pJsonRoot=cJSON_CreateObject();
	 cJSON_AddStringToObject(pJsonRoot, "type", "gethostinfo");
	 char* c=cJSON_Print(pJsonRoot);
	 cJSON_Delete(pJsonRoot);
	 return c;
}

static char* jsonsetlancfg(const char* ip,const char* mask,const char* dbcp,const char* startip,const char* endip)
{

	 cJSON * pJsonRoot = NULL;
	 pJsonRoot=cJSON_CreateObject();
	 cJSON_AddStringToObject(pJsonRoot, "type", "setlancfg");
	 cJSON_AddStringToObject(pJsonRoot, "lanip",ip);
	 cJSON_AddStringToObject(pJsonRoot, "lanmsk",mask);
	 cJSON_AddStringToObject(pJsonRoot, "landhcp",dbcp);
	 cJSON_AddStringToObject(pJsonRoot, "startip",startip);
	 cJSON_AddStringToObject(pJsonRoot, "endip", endip);
	 char* c=cJSON_Print(pJsonRoot);
	 cJSON_Delete(pJsonRoot);
	 return c;

}

static char* jsonsetwlancfg(const char* wlanssid,const char* wlanpsw,const char* wlanpswmode,const char* wlanpswencry)
{

	 cJSON * pJsonRoot = NULL;
	 pJsonRoot=cJSON_CreateObject();
	 cJSON_AddStringToObject(pJsonRoot, "type", "setwlancfg");
	 cJSON_AddStringToObject(pJsonRoot, "lanip",wlanssid);
	 cJSON_AddStringToObject(pJsonRoot, "lanmsk",wlanpsw);
	 cJSON_AddStringToObject(pJsonRoot, "landhcp",wlanpswmode);
	 cJSON_AddStringToObject(pJsonRoot, "startip",wlanpswencry);
	 char* c=cJSON_Print(pJsonRoot);
	 cJSON_Delete(pJsonRoot);
	 return c;

}

static char* jsonsetarpbind(const char* mac,const char* ip,int bind)
{

	 cJSON * pJsonRoot = NULL;
	 pJsonRoot=cJSON_CreateObject();
	 cJSON_AddStringToObject(pJsonRoot, "type", "setarpbind");
	 cJSON_AddStringToObject(pJsonRoot, "mac",mac);
	 cJSON_AddStringToObject(pJsonRoot, "ip",ip);
	 cJSON_AddNumberToObject(pJsonRoot, "bind",bind);
	 char* c=cJSON_Print(pJsonRoot);
	 cJSON_Delete(pJsonRoot);
	 return c;

}



static char* jsonSetWancfg(const char* wanmode,const char* wanip,const char* wanmask,const char* wangw,const char* dns1,const char* dns2){

	 cJSON * pJsonRoot = NULL;
	 pJsonRoot=cJSON_CreateObject();
	 cJSON_AddStringToObject(pJsonRoot, "type", "setwancfg");
	 cJSON_AddStringToObject(pJsonRoot, "wanip",wanmode);
	 cJSON_AddStringToObject(pJsonRoot, "wanip",wanip);
	 cJSON_AddStringToObject(pJsonRoot, "wanmask",wanmask);
	 cJSON_AddStringToObject(pJsonRoot, "wangw",wangw);
	 cJSON_AddStringToObject(pJsonRoot, "dns1",dns1);
	 cJSON_AddStringToObject(pJsonRoot, "dns2", dns2);
	 char* c=cJSON_Print(pJsonRoot);
	 cJSON_Delete(pJsonRoot);
	 return c;
}

#endif /* DATALINK_H_ */
